﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace SSS.AlphaNN.Components
{
    /// <summary>
    /// 
    /// </summary>
    public enum SlotStateEnum
    {
        /// <summary>
        /// 
        /// </summary>
        Selected,
        /// <summary>
        /// 
        /// </summary>
        Marked,
        /// <summary>
        /// 
        /// </summary>
        None
    }

    public partial class Slot : UserControl
    {
        /// <summary>
        /// Конструктор
        /// </summary>
        public Slot()
        {
            InitializeComponent();
            Identificator = counter;
            this.BackColor = Color.FromArgb( 128, Color.Orange );
            counter++;
            this.MouseEnter += new EventHandler( Slot_MouseEnter );
            this.MouseLeave += new EventHandler( Slot_MouseLeave );

            NoneScaleSize = this.Size;
            //id++;

            this.Visible = false;
            //this.Resize += new EventHandler( Slot_Resize );
        }



        /*
         * Props
         * =========================
         */


        /// <summary>
        /// 
        /// </summary>
        private SlotStateEnum curr_slot_sate = SlotStateEnum.None;

        /// <summary>
        /// 
        /// </summary>
        public SlotStateEnum SlotState
        {
            get
            {
                return curr_slot_sate;
            }
            set
            {
                curr_slot_sate = value;

                switch ( value )
                {
                    case SlotStateEnum.Selected:

                        break;

                    case SlotStateEnum.Marked:

                        Mark = true;

                        break;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private Size noneScaleSize;

        /// <summary>
        /// 
        /// </summary>
        public Size NoneScaleSize
        {
            get
            {
                return noneScaleSize;
            }
            set
            {
                noneScaleSize = value;
                //this.Refresh();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public float Zoom
        {
            get
            {
                return ( this.Parent as Dummy ).Zoom;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private bool marked = false;

        /// <summary>
        /// 
        /// </summary>
        public bool Mark
        {
            set
            {
                this.BackColor = ( marked = value ) ? Color.Orange : Color.FromArgb( 128, Color.Orange );
            }
            get
            {
                return marked;
            }
        }

        


        /*
         * Methods
         * ===========================
         */ 

        //public void SlotResize()
        //{
        //    float zoom = Zoom;

        //    this.Size = new Size( ( int )( this.NoneScaleSize.Width + this.NoneScaleSize.Width * zoom / 100 ), ( int )( this.NoneScaleSize.Height + this.NoneScaleSize.Height * zoom / 100 ) );
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Slot_MouseLeave( object sender, EventArgs e )
        {
            //MessageBox.Show( this.Name );
            //( this.Parent as Dummy ).ShowSlots( false, false );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Slot_MouseEnter( object sender, EventArgs e )
        {
            //( this.Parent as Dummy ).selfShadeTimer.Dispose();
        }       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Slot_Click( object sender, EventArgs e )
        {
            WorkGround parent_wrkGround = (WorkGround) this.Parent;


            if ( parent_wrkGround.markedSlots.Count != 0 )
            {
                //не соединять в пределах одного родителя
                if ( this.Owner == ( this.Parent as WorkGround ).markedSlots.Peek().Owner )
                {
                    return;
                }

                if ( ( this.Owner is Source ) || ( this.Owner is Sink ) )
                {
                    goto mark1;
                }
                if ( ( ( this.Parent as WorkGround ).markedSlots.Peek().Owner is Source ) || ( ( this.Parent as WorkGround ).markedSlots.Peek().Owner is Sink ) )
                {
                    goto mark1;
                }


                    

                if ( ( this.Name == "slot1" ) && ( ( this.Parent as WorkGround ).markedSlots.Peek().Name == "slot1" ) )
                {
                    return;
                }
                if ( ( this.Name == "slot2" ) && ( ( this.Parent as WorkGround ).markedSlots.Peek().Name == "slot2" ) )
                {
                    return;
                }

                

            }

            mark1:

            parent_wrkGround.markedSlots.Push( this );

            if ( parent_wrkGround.markedSlots.Count == 2 )
            {
                parent_wrkGround.markedSlots.ToArray()[0].Mark = false;
                parent_wrkGround.markedSlots.ToArray()[1].Mark = false;

                parent_wrkGround.markedSlots.ToArray()[0].Visible = false;
                parent_wrkGround.markedSlots.ToArray()[1].Visible = false;

                parent_wrkGround.MakeLink();
            }
            else
            {
                SlotState = SlotStateEnum.Marked;
            }
        }




    }
}
